<template>
  <div class="login_main">
    <el-row>
      <el-col :span="12"><div class="bg"></div></el-col>
      <el-col :span="6" :offset="3">
        <div class="form_box">
          <h3>{{ loginFlg ? '登录' : '注册' }}</h3>
          <el-form
            v-if="loginFlg"
            :model="form"
            :rules="rules"
            label-width="auto"
            style="max-width: 600px"
          >
            <el-form-item label="" prop="name">
              <el-input v-model="form.name" style="width: 280px" placeholder="请输入用户名">
                <template #prefix>
                  <el-icon class="el-input__icon"><User /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item label="" prop="password">
              <el-input
                v-model="form.password"
                style="width: 280px"
                type="password"
                placeholder="请输入密码"
                show-password
              >
                <template #prefix>
                  <el-icon class="el-input__icon"><Lock /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <div class="form_info">
              <el-checkbox v-model="checked" label="记住我" size="large" />
              <span class="f_12">忘记密码？</span>
            </div>
          </el-form>

          <el-form v-else :model="form" :rules="rules" label-width="auto" style="max-width: 600px">
            <el-form-item label="" prop="name">
              <el-input v-model="form.name" style="width: 280px" placeholder="请输入用户名">
                <template #prefix>
                  <el-icon class="el-input__icon"><User /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item label="" prop="password">
              <el-input
                v-model="form.password"
                style="width: 280px"
                type="password"
                placeholder="请输入密码"
                show-password
              >
                <template #prefix>
                  <el-icon class="el-input__icon"><Lock /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item label="" prop="passwordTwo">
              <el-input
                v-model="form.passwordTwo"
                style="width: 280px"
                type="password"
                placeholder="请再次输入密码"
                show-password
              >
                <template #prefix>
                  <el-icon class="el-input__icon"><Lock /></el-icon>
                </template>
              </el-input>
            </el-form-item>
          </el-form>

          <el-button class="btn_class" type="primary" @click="onSubmit">{{
            loginFlg ? '登录' : '注册'
          }}</el-button>

          <div class="flex_a cursor" v-if="loginFlg" @click="operate(1)">
            <span class="text_color magin_r_5">注册</span>
            <el-icon color="#919398"><Right /></el-icon>
          </div>

          <div class="flex_a cursor" v-else @click="operate(2)">
            <el-icon color="#919398"><Back /></el-icon>
            <span class="text_color magin_l_5">返回</span>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
  
<script setup>
import { ref } from 'vue'
import { userRegisterService, uerLoginService } from '../../api/User'
import { ElMessage } from 'element-plus'
import router from '@/router'
import { useUserStore } from '@/stores/index.js'
const form = ref({
  name: '',
  password: '',
  passwordTwo: ''
})
const checked = ref(false)
const loginFlg = ref(true)

const validatePass = (rule, value, callback) => {
  if (loginFlg.value) {
    callback()
  } else if (value === '') {
    callback(new Error('请输入密码'))
  } else if (value !== form.value.password) {
    callback(new Error('两次密码输入不一致，请重新输入'))
  } else {
    callback()
  }
}

const rules = ref({
  name: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '长度3-10', trigger: 'blur' }
  ],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  passwordTwo: [{ validator: validatePass, trigger: 'blur' }]
})

const operate = (val) => {
  form.value = {
    name: '',
    password: '',
    passwordTwo: ''
  }
  if (val === 1) {
    // 代表点击注册
    loginFlg.value = false
  } else {
    loginFlg.value = true
  }
}
const onSubmit = () => {
  if (loginFlg.value) {
    onLogin()
  } else {
    onRegister()
  }
}

// 注册操作
const onRegister = async () => {
  await form.value.validate()
  const res = await userRegisterService(form.value)
  if (res.status === 0) {
    ElMessage({
      message: '注册成功！',
      type: 'success'
    })
    setTimeout(() => {
      loginFlg.value = true
    }, 1000)
  } else {
    ElMessage.error(res.message)
  }
}

// 登录操作
const onLogin = async () => {
  const res = await uerLoginService(form.value)
  if (res.status === 0) {
    useUserStore().setToken(res.token)
    localStorage.setItem('userInfo', JSON.stringify(res.userInfo))
    ElMessage({
      message: '登录成功！',
      type: 'success'
    })
    setTimeout(() => {
      router.push('/')
    }, 1000)
  } else {
    ElMessage.error(res.message)
  }
}
</script>
  
<style scoped lang="scss">
.login_main {
  .bg {
    height: 100vh;
    background-image: url('../../assets/head.jpg');
    background-size: cover;
    background-position: 50% 50%;
    background-repeat: no-repeat;
    border-radius: 0 10px 10px 0;
  }

  .form_box {
    margin: 50% 0;
  }

  .form_info {
    width: 280px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    span {
      color: #7cabf8;
    }
  }

  .btn_class {
    width: 280px;
    margin: 30px 0;
  }
}
</style>
  